package cn.wxl.mymeituan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wxl.mymeituan.R;
import cn.wxl.mymeituan.bean.FoodBean;
import com.bumptech.glide.Glide;

import java.math.BigDecimal;
import java.util.List;

public class OrderAdapter extends BaseAdapter {
    private Context mContext;
    private List<FoodBean> fbl;
    public OrderAdapter(Context context) {
        this.mContext = context;
    }
    /**
     * 设置数据更新界面
     */
    public void setData(List<FoodBean> fbl) {
        this.fbl = fbl;
        notifyDataSetChanged();
    }
    /**
     * 获取Item的总数
     */
    @Override
    public int getCount() {
        return fbl == null ? 0 : fbl.size();
    }
    /**
     * 根据position得到对应Item的对象
     */
    @Override
    public FoodBean getItem(int position) {
        return fbl == null ? null : fbl.get(position);
    }
    /**
     * 根据position得到对应Item的id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 得到相应position对应的Item视图，position是当前Item的位置，
     * convertView参数是滚出屏幕的Item的View
     */
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        //复用convertView
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.order_item, null);
            vh.food_name = (TextView) convertView.findViewById(R.id.food_name);
            vh.car_count = (TextView) convertView.findViewById(R.id.car_count);
            vh.or_food_cost = (TextView) convertView.findViewById(R.id.or_food_cost);
            vh.food_image = (ImageView) convertView.findViewById(R.id.food_image);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //获取position对应的Item的数据对象
        final FoodBean foodBean = getItem(position);
        if (foodBean != null) {
            vh.food_name.setText(foodBean.getFoodName());
            vh.car_count.setText("x"+foodBean.getCount());
            vh.or_food_cost.setText("￥"+foodBean.getPrice().multiply(BigDecimal.valueOf(foodBean.getCount())));
            Glide.with(mContext)
                    .load(foodBean.getFoodImage())
                    .into(vh.food_image);
        }
        return convertView;
    }
    class ViewHolder {
        public TextView food_name, car_count, or_food_cost;
        public ImageView food_image;
    }
}
